using System;

using SdlDotNet.Core;
using SdlDotNet.Input;

using Game.Output;

namespace Game.Input.Devices
{
	public static class CKeyboardHandler
	{
		public struct structCursorKeys
		{
			public bool up;
			public bool down;
			public bool left;
			public bool right;
			
			public bool escape;
		}
		
		private static structCursorKeys	m_pressedKeys;
	
		public static void Initialize()
		{
			CConsole.ReportClassInit(System.Reflection.MethodInfo.GetCurrentMethod().DeclaringType.Name);
			
			Events.KeyboardDown +=
				new EventHandler<KeyboardEventArgs>(KeyboardEventHandler);
			Events.KeyboardUp +=
				new EventHandler<KeyboardEventArgs>(KeyboardEventHandler);
		}
		
		private static void KeyboardEventHandler(object sender, KeyboardEventArgs args)
		{
			switch (args.Key)
			{
				case Key.W:
					m_pressedKeys.up = args.Down;
					break;
				case Key.S:
					m_pressedKeys.down = args.Down;
					break;
				case Key.A:
					m_pressedKeys.left = args.Down;
					break;
				case Key.D:
					m_pressedKeys.right = args.Down;
					break;
				case Key.Escape:
					m_pressedKeys.escape = args.Down;
					break;
			}
		}
		
		public static void Process()
		{
			if (m_pressedKeys.up)
			{
				if (m_pressedKeys.right)
					CNavigation.Scroll(CNavigation.Direction.NORTH_EAST);	
				else if (m_pressedKeys.left)
					CNavigation.Scroll(CNavigation.Direction.NORTH_WEST);	
				else
					CNavigation.Scroll(CNavigation.Direction.SOUTH);	
			}
			
			if (m_pressedKeys.down)
			{
				if (m_pressedKeys.right)
					CNavigation.Scroll(CNavigation.Direction.SOUTH_EAST);	
				else if (m_pressedKeys.left)
					CNavigation.Scroll(CNavigation.Direction.SOUTH_WEST);	
				else
					CNavigation.Scroll(CNavigation.Direction.SOUTH);	
			}
			
			if (m_pressedKeys.right)
				CNavigation.Scroll(CNavigation.Direction.EAST);
			
			if (m_pressedKeys.left)
				CNavigation.Scroll(CNavigation.Direction.WEST);
		
			if (m_pressedKeys.escape)
				Events.QuitApplication();
		}
	}
}
